package com.bigknow.appstore.web.base.controller;

import com.bigknow.appstore.web.app.service.impl.AppStoreService;
import com.bigknow.appstore.web.app.vo.AppCategoryVO;
import com.bigknow.appstore.web.app.vo.AppVO;
import com.bigknow.appstore.web.console.app.Comment;
import com.bigknow.appstore.web.console.app.Tag;
import com.bigknow.appstore.web.framework.controller.BaseController;
import org.apache.tools.ant.util.DateUtils;
import org.apache.tools.ant.util.StringUtils;
import org.mongodb.morphia.Datastore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by zhangwei on 2016/9/19.
 */
@Controller
@RequestMapping(value = "/app")
public class AppStoreController extends BaseController {

    @Autowired
    private AppStoreService appStoreService;


    @Autowired
    private Datastore ds;

    @RequestMapping
    public ModelAndView init() {
        ModelAndView view = new ModelAndView("/app/app_index");
        view.addObject("categories", appStoreService.getCategories());
        view.addObject("top10", appStoreService.top(10));
        view.addObject("hot", appStoreService.top(8));
        view.addObject("commonApps", appStoreService.getCommonApps());
        view.addObject("recommendApps", appStoreService.getRecommendApps());
        view.addObject("banners", appStoreService.getBanners());
        view.addObject("isLogin", appStoreService.isLogin());
        view.addObject("userName", appStoreService.getUser());
        return view;
    }

    //获取应用排行
    @RequestMapping("/top10")
    @ResponseBody
    public List<AppVO> top10() {
        return appStoreService.top(10);
    }

    //获取热门应用
    @RequestMapping("/hot")
    @ResponseBody
    public List<AppVO> hot() {
        return appStoreService.top(8);
    }


    @RequestMapping("/getTags")
    @ResponseBody
    public List<Tag> getTags() {
        return appStoreService.getTags();
    }

    @RequestMapping("/detail")
    public ModelAndView detail(String id) {
        ModelAndView view = new ModelAndView("/app/app_detail");
        view.addObject("top10", appStoreService.top(10));
        view.addObject("app", appStoreService.getApp(id));
        view.addObject("recommendApps", appStoreService.getRecommendApps());
        view.addObject("relatedApps", appStoreService.getRelatedApps(id));
        view.addObject("isLogin", appStoreService.isLogin());
        view.addObject("userName", appStoreService.getUser());
        return view;
    }

    @RequestMapping("/toBanner")
    public String toBanner(String id) {
        List<String> appIds = appStoreService.getBannerAppIds(id);
        if (appIds != null && appIds.size() == 1) {
            return "redirect:/app/detail?id=" + appIds.get(0);
        } else if (appIds != null && appIds.size() > 0) {
            return "redirect:/app/classify?appIds=" + String.join(",", (String[]) appIds.toArray(new String[appIds.size()]));
        } else {
            return "redirect:/app";
        }
    }


    @RequestMapping("/classify")
    public ModelAndView classify(String id, String appName, String appIds, String tag) {
        ModelAndView view = new ModelAndView("/app/app_classify");
        view.addObject("isLogin", appStoreService.isLogin());
        view.addObject("userName", appStoreService.getUser());
        view.addObject("category", appStoreService.getCategory(id));
        view.addObject("top10", appStoreService.top(10));
        view.addObject("recommendApps", appStoreService.getRecommendApps());
        view.addObject("tags", appStoreService.getAllTags());
        view.addObject("categoryId", id);
        view.addObject("appName", appName);
        view.addObject("appIds", appIds);
        if(!org.apache.commons.lang3.StringUtils.isEmpty(tag)){
            view.addObject("tagId", appStoreService.getTagId(tag));
        }
        return view;
    }

    @RequestMapping("/appList")
    public ModelAndView getApps(String[] tagIds, String orderType, String categoryId, String appName, String[] appIds) {
        ModelAndView view = new ModelAndView("/app/appList");
        if (appIds == null || appIds.length == 0) {
            view.addObject("apps", appStoreService.getAppsByCondition(tagIds, orderType, categoryId, appName));
        } else {
            view.addObject("apps", appStoreService.getAppsByIds(Arrays.asList(appIds)));
        }

        return view;
    }
}
